﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GraphDrawer.GraphProvider;

namespace GraphDrawer {
	public partial class SimulationVisualization : UserControl {
		public Dictionary<GraphElement, GraphLayout.Interface.VisualItem> map = new Dictionary<GraphElement, GraphLayout.Interface.VisualItem>();

		private SpreadVisualization menu;
		private GleeGraphDrawer layout = null;
		private VirtualEarthControl earth = null;

		double preferredWidth;
		double preferredHeight;

		public SimulationVisualization(SpreadVisualization menu) {
			InitializeComponent();
			
			this.menu = menu;

			this.Control.SelectionChanged += new SelectionChangedEventHandler(Control_SelectionChanged);
			this.LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);
		}

		public void EnableNext() {
			menu.EnableNext();
		}

		public SetupSimulation GetSetup() {
			return menu.GetSetup();
		}

		public static SolidColorBrush GetColorForDisease(String Disease) {
			return SpreadVisualization.GetColorForDisease(Disease);
		}

		public String[] GetAvailableStates() {
			return menu.GetAvailableStates();
		}

		public void Init() {
			if (Control.SelectedItem == GleeGraphDrawer)
				GleeGraphDrawer_Selected();
			else
				VirtualEarthControl_Selected();
		}

		void Control_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			if (Control.SelectedItem == GleeGraphDrawer)
				GleeGraphDrawer_Selected();
			else
				VirtualEarthControl_Selected();
		}

		void GleeGraphDrawer_Selected() {
			menu.DisableNext();
			menu.GetSetupSimulation();
			earth = null;
			
			layout = new GleeGraphDrawer(this);
			layout.Width = preferredWidth;
			layout.Height = preferredHeight;
			this.GleeGraphDrawer.Content = layout;
		}

		void VirtualEarthControl_Selected() {
			menu.DisableNext();
			menu.GetSetupSimulation();
			layout = null;

			earth = new VirtualEarthControl(this);
			earth.Width = preferredWidth;
			earth.Height = preferredHeight;
			this.VirtualEarthControl.Content = earth;
		}

		void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e) {
			preferredWidth = e.NewSize.Width - 10;
			preferredHeight = e.NewSize.Height - GleeGraphDrawer.Height;
			if (layout != null) {
				layout.Width = e.NewSize.Width - 10;
				layout.Height = e.NewSize.Height - GleeGraphDrawer.Height;
			}
			if (earth != null) {
				earth.Width = e.NewSize.Width - 10;
				earth.Height = e.NewSize.Height - VirtualEarthControl.Height;
			}
		}
	}
}
